package com.netty.unit1nio.c1;

import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

@Slf4j
public class ByteBufferDemo {
    public static void main(String[] args)  {
//使用channel(读写数据的双向通道)写入ByteBuffer
        try{
            FileChannel channel = new FileInputStream("F:\\code\\netty_code\\netty\\src\\main\\resources\\data.txt").getChannel();
            //分配空间
            ByteBuffer buf = ByteBuffer.allocate(10);
            //将数据写入到ByteBuffer
            int read = channel.read(buf);//读取到的字节数,读取不到输出-1
            log.debug("{}",read);
            //打印buffer内容
            buf.flip();//切换到写模式，实际是改变指针位置
            buf.mark();//作一个标记
            while(buf.hasRemaining()){
                byte b = buf.get();//无参get会让指针往后走
                log.debug("{}",(char)b);
            }
buf.reset();//回到标记位置
            byte b = buf.get();//无参get会让指针往后走
            log.debug("{}",(char)b);
            //字符串和ByteBuffer互转
            ByteBuffer buf2 = StandardCharsets.UTF_8.encode("hello");
            while(buf2.hasRemaining()){
                byte c=buf2.get();
                System.out.print((char)c);
            }
            System.out.println("============================");
            //
        }catch (Exception e){
            e.printStackTrace();
        }
//分散读
        try{
            RandomAccessFile randomAccessFile = new RandomAccessFile("F:\\code\\netty_code\\netty\\src\\main\\resources\\cd.txt","rw");
            ByteBuffer buffer1 = ByteBuffer.allocate(3);
            ByteBuffer buffer2 = ByteBuffer.allocate(3);
            ByteBuffer buffer3 = ByteBuffer.allocate(5);
            FileChannel channel = randomAccessFile.getChannel();
            //channel.read
            channel.read(new ByteBuffer[]{buffer1,buffer2,buffer3});
            ByteBufferUtil.debugAll(buffer1);
            ByteBufferUtil.debugAll(buffer2);
            ByteBufferUtil.debugAll(buffer3);
        }catch (IOException e){
            e.printStackTrace();
        }
        //集中写
        try{
            RandomAccessFile randomAccessFile = new RandomAccessFile("F:\\code\\netty_code\\netty\\src\\main\\resources\\cd.txt","rw");
            FileChannel channel = randomAccessFile.getChannel();
            channel.position(11);//移动
            ByteBuffer d = ByteBuffer.allocate(4);
            ByteBuffer e = ByteBuffer.allocate(4);
            d.put(new byte[]{'f', 'o', 'u', 'r'});
            e.put(new byte[]{'f', 'i', 'v', 'e'});
            ByteBufferUtil.debugAll(d);
            ByteBufferUtil.debugAll(e);
            //集中写
            channel.write(new ByteBuffer[]{d,e});
        }catch (IOException e){
            e.printStackTrace();
        }
        ByteBuffer source = ByteBuffer.allocate(32);
        //                     11            24
        source.put("Hello,world\nI'm zhangsan\nHo".getBytes());
        split(source);

        source.put("w are you?\nhaha!\n".getBytes());
        split(source);



    }
static void split(ByteBuffer source){
        source.flip();//切换到读模式
for(int i=0;i<source.limit();i++){
    if(source.get(i)=='\n'){
        int length=i-source.position()+1;
        ByteBuffer target = ByteBuffer.allocate(length);
        //将source内容写入到target
        for(int j=0;j<length;j++){
            target.put(source.get());//同时移动指针
        }
        ByteBufferUtil.debugAll(target);
    }
}
    source.compact();//切换到写模式
}
}
